{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "266a69b3-b5d3-4636-9d91-aa6bc9b2a988",
   "metadata": {},
   "source": [
    "# 流程控制\n",
    "\n",
    "流程控制是编程中非常核心的一个概念，它允许程序根据条件和逻辑判断来选择执行特定的代码块。在Python中，流程控制主要通过条件语句和循环语句来实现，这些结构让程序具有了决策能力和重复执行任务的能力，是构建有效且高效程序的基石。\n",
    "\n",
    "### 条件语句\n",
    "\n",
    "条件语句允许程序根据条件的真假执行不同的操作。Python中的条件语句主要包括`if`、`if-else`和`if-elif-else`结构。这些结构让程序能够在多种条件和情况下作出选择，执行相应的代码块。\n",
    "\n",
    "### 循环语句\n",
    "\n",
    "循环语句使程序能够重复执行一段代码，直到满足特定的条件。Python提供了两种循环语句：`for`循环和`while`循环。`for`循环通常用于遍历序列（如列表或字符串）中的每个元素，而`while`循环则根据条件的真假重复执行代码块。\n",
    "\n",
    "### 跳转语句\n",
    "\n",
    "跳转语句，包括`break`和`continue`，用于在循环中改变执行流程。`break`用来立即退出循环，而`continue`则跳过当前循环的剩余代码，直接开始下一次循环迭代。\n",
    "\n",
    "### 循环嵌套\n",
    "\n",
    "循环嵌套是指在一个循环内部再嵌套一个或多个循环。这种结构常用于处理多维数据结构，如二维列表（矩阵）等。通过循环嵌套，可以实现更复杂的数据处理和计算。\n",
    "\n",
    "### 重要性\n",
    "\n",
    "流程控制是程序设计中不可或缺的一部分。通过合理地使用条件语句和循环语句，可以构建出逻辑严密、功能强大的程序。理解和掌握流程控制对于学习编程语言来说是非常重要的，它不仅影响程序的结构和逻辑，还关系到程序的效率和执行性能。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6177029-211f-4d61-b1ce-417c92de690e",
   "metadata": {},
   "source": [
    "### 1. 掌握if语句的用法\n",
    "\n",
    "`if`语句是编程中用来进行条件判断的基本结构。它允许程序根据条件的真假执行不同的代码段。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  if 条件表达式:\n",
    "      代码块\n",
    "  ```\n",
    "\n",
    "  如果`条件表达式`为真（`True`），则执行`代码块`。\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4386a1b7-94ab-40e2-835e-e8cd9f52bf18",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你已经成年了。\n"
     ]
    }
   ],
   "source": [
    "# 判断年龄是否达到18岁\n",
    "age = 18\n",
    "if age >= 18:\n",
    "    print(\"你已经成年了。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "285cfddc-11ac-4b80-826a-94bece506dcd",
   "metadata": {},
   "source": [
    "### 2. 掌握if-else语句的用法\n",
    "\n",
    "`if-else`结构用于在两种情况之间做出选择：如果条件为真，执行一段代码；如果条件为假，执行另一段代码。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  if 条件表达式:\n",
    "      代码块1\n",
    "  else:\n",
    "      代码块2\n",
    "  ```\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "65858390-feed-4dfd-84c1-2bbf00148b30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个1到7的数字来表示星期几： 6\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这一天不是工作日。\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入\n",
    "day_number = int(input(\"请输入一个1到7的数字来表示星期几：\"))\n",
    "\n",
    "# 判断星期几是否是工作日并输出结果\n",
    "if day_number >= 1 and day_number <= 5:  # 1到5代表星期一到星期五\n",
    "    print(\"这一天是工作日。\")\n",
    "else:\n",
    "    print(\"这一天不是工作日。\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0f8d060a-7b99-4532-b5ef-6a06220888d4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不及格\n"
     ]
    }
   ],
   "source": [
    "# 判断考试成绩是否及格\n",
    "score = 59\n",
    "if score >= 60:\n",
    "    print(\"及格\")\n",
    "else:\n",
    "    print(\"不及格\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "da9425b1-dcfe-41ec-a0a7-a07877d88c15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个整数： 11111\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是回文数。\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入\n",
    "num = input(\"请输入一个整数：\")\n",
    "\n",
    "# 判断是否是回文数\n",
    "if num == num[::-1]:\n",
    "    print(\"是回文数。\")\n",
    "else:\n",
    "    print(\"不是回文数。\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcddd5f9-c2cb-41e2-b3b8-bfb906745587",
   "metadata": {},
   "source": [
    "### 3. 掌握if-elif-else语句的用法\n",
    "\n",
    "`if-elif-else`结构允许你根据多个条件判断执行不同的代码段。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  \n",
    "  if 条件表达式1:\n",
    "      代码块1\n",
    "  elif 条件表达式2:\n",
    "      代码块2\n",
    "  else:\n",
    "      代码块3\n",
    "  ```\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "257be085-704f-4a7f-a39a-7ba6cb382c68",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入成绩： 79\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中等\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入成绩：\"))\n",
    "\n",
    "if score >= 90:\n",
    "    print(\"优秀\")\n",
    "elif score >= 80:\n",
    "    print(\"良好\")\n",
    "elif score >= 70:\n",
    "    print(\"中等\")\n",
    "elif score >= 60:\n",
    "    print(\"及格\")\n",
    "else:\n",
    "    print(\"不及格\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e94b2b97-e480-4859-b18a-c7027cdc67a1",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d9fcbef2-9a6d-4d83-ae86-25620afaa73d",
   "metadata": {},
   "source": [
    "\n",
    "### 4. 掌握if语句嵌套的用法\n",
    "\n",
    "`if`语句的嵌套使用是将一个`if`语句的整个体（包括它的`else`部分）放入另一个`if`或`else`代码块中。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  if 条件表达式1:\n",
    "      if 条件表达式2:\n",
    "          代码块1\n",
    "      else:\n",
    "          代码块2\n",
    "  ```\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cdcb9a21-2a66-4981-a2a1-92df07f0c367",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数字在10到20之间\n"
     ]
    }
   ],
   "source": [
    "# 判断一个数字是否既大于10也小于20\n",
    "num = 15\n",
    "if num > 10:\n",
    "    if num < 20:\n",
    "        print(\"数字在10到20之间\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ea299875-d088-486b-91df-995a64b167b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纪念日已经过去了 804 天。\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "\n",
    "# 定义纪念日\n",
    "anniversary_date = datetime.datetime(2022, 1, 1)\n",
    "\n",
    "# 获取今天的日期\n",
    "today = datetime.datetime.now()\n",
    "\n",
    "# 计算纪念日到今天的天数\n",
    "days_since_anniversary = (today - anniversary_date).days\n",
    "\n",
    "# 判断是否已经过了纪念日\n",
    "if days_since_anniversary < 0:\n",
    "    print(\"距离纪念日还有\", -days_since_anniversary, \"天。\")\n",
    "elif days_since_anniversary == 0:\n",
    "    print(\"今天是纪念日！\")\n",
    "else:\n",
    "    print(\"纪念日已经过去了\", days_since_anniversary, \"天。\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49c02987-f679-4610-b127-9bc4c7b78531",
   "metadata": {},
   "source": [
    "### 5. 掌握for语句\n",
    "\n",
    "`for`语句用于遍历任何序列的项目，如一个列表或者一个字符串。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  for 变量 in 序列:\n",
    "      代码块\n",
    "  ```\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "42e205f9-675c-4e1b-8f03-9aed0bdae9c4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "# 遍历列表中的每个元素\n",
    "for fruit in [\"apple\", \"banana\", \"cherry\"]:\n",
    "    print(fruit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6c9310e6-214f-4a89-a90d-a81ce92c3b07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# 定义一个列表\n",
    "my_list = [1, 2, 3, 4, 5]\n",
    "\n",
    "# 使用for循环遍历列表中的每个元素\n",
    "for item in my_list:\n",
    "    print(item)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0e757ab4-f5d8-4d6b-91a4-5670c5f4444c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      ",\n",
      " \n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "# 定义一个字符串\n",
    "my_string = \"Hello, world!\"\n",
    "\n",
    "# 使用for循环遍历字符串中的每个字符\n",
    "for char in my_string:\n",
    "    print(char)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35785e82-9de9-4319-9b94-ef0706bbb07b",
   "metadata": {},
   "source": [
    "### 6. 掌握range()函数的用法\n",
    "\n",
    "`range()`函数用于生成一个整数序列。这个序列可以用在`for`循环中来进行迭代。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  range(start, stop[, step])\n",
    "  ```\n",
    "\n",
    "  其中`start`是序列的开始值，`stop`是序列结束的边界值，`step`是可选的步长。\n",
    "\n",
    "- **例子*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "63115f87-7399-4123-8281-dd9ad8b4f8da",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'rang' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[17], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mrang\u001b[49m(\u001b[38;5;241m5\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'rang' is not defined"
     ]
    }
   ],
   "source": [
    "range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "678c1e74-d1a6-419a-9ad9-2dcbf70c7855",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 5)\n",
      "0\n",
      "range(0, 5)\n",
      "1\n",
      "range(0, 5)\n",
      "2\n",
      "range(0, 5)\n",
      "3\n",
      "range(0, 5)\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 使用range()生成0到4的数字\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2fa2fda6-02b0-46f6-882e-359353822c62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# 使用for循环和range函数打印出自定义范围的整数序列\n",
    "for i in range(1, 10, 2):  # 从1开始，到10结束，步长为2\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65926bb8-21d2-4c73-b706-e1e5f46d5dce",
   "metadata": {},
   "source": [
    "### 7. 掌握while语句的用法\n",
    "\n",
    "`while`循环用于在条件为真的情况下重复执行一个代码块。\n",
    "\n",
    "- **基本用法**：\n",
    "\n",
    "  ```\n",
    "  while 条件表达式:\n",
    "      代码块\n",
    "  ```\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "98667b96-6ca2-4c9e-b6f7-c540029d925a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# 计数到5\n",
    "count = 1\n",
    "while count <= 5:\n",
    "    print(count)\n",
    "    count += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90433464-eb04-4fc6-9ab5-b48cb6d58312",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "# 生成一个随机数作为答案\n",
    "answer = random.randint(1, 100)\n",
    "\n",
    "# 循环猜数字直到猜对为止\n",
    "while True:\n",
    "    # 获取用户输入的猜测数字\n",
    "    guess = int(input(\"猜一个1到100之间的数字：\"))\n",
    "\n",
    "    # 判断用户猜测的数字与答案的关系\n",
    "    if guess < answer:\n",
    "        print(\"太小了，请继续猜。\")\n",
    "    elif guess > answer:\n",
    "        print(\"太大了，请继续猜。\")\n",
    "    else:\n",
    "        print(\"恭喜你，猜对了！\")\n",
    "        break  # 猜对了就跳出循环\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f691fa6b-6074-4c72-a33c-861cac39e311",
   "metadata": {},
   "source": [
    "### 8. 掌握跳转语句的用法\n",
    "\n",
    "`break`和`continue`是在循环中用来改变循环的执行流程的两个语句。`break`用于完全终止循环，而`continue`用于跳过当前循环的剩余部分，直接开始下一次循环。\n",
    "break 和 continue 是 Python 中控制循环行为的关键字，它们通常与 for 或 while 循环一起使用。\n",
    "\n",
    "break:\n",
    "\n",
    "break 关键字用于完全退出当前循环，不再执行循环中剩余的代码，直接跳出循环体，执行循环后的代码。\n",
    "当程序遇到 break 语句时，循环立即终止，不会执行剩余的循环体中的代码，程序将继续执行循环后的代码。\n",
    "break 通常与条件语句结合使用，用于提前结束循环，例如当找到目标值时，不再需要continue:\n",
    "\n",
    "continue 关键字用于跳过当前循环中的剩余代码，继续下一次循环的执行。\n",
    "当程序遇到 continue 语句时，循环会立即跳到下一次迭代，不再执行当前迭代中 continue 语句之后的代码。\n",
    "continue 主要用于循环中的条件判断，用于提前结束当前迭代，直接进行下一次迭代。环。\n",
    "示例：\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "29020b9c-6e74-4250-8509-0d927cf2232d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 使用break退出循环\n",
    "for i in range(10):\n",
    "    if i == 5:\n",
    "        break\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "539d2177-a2da-48c0-b298-f1b48b35c138",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 使用continue跳过某些迭代\n",
    "for i in range(10):\n",
    "    if i % 2 == 0:\n",
    "        continue\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36b282b2-89c9-4911-a058-c28aa2593dd0",
   "metadata": {},
   "source": [
    "### 9. 掌握循环嵌套的用法\n",
    "\n",
    "循环嵌套是指在一个循环内部使用另一个循环，这在处理多维数据结构时非常有用。\n",
    "\n",
    "- **例子**："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3e9b0fb7-59fc-45d2-ab2e-03d50592ca2a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 \n",
      "4 5 6 \n",
      "7 8 9 \n"
     ]
    }
   ],
   "source": [
    "# 使用嵌套循环打印2D矩阵\n",
    "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "for row in matrix:\n",
    "    for item in row:\n",
    "        print(item, end=' ')\n",
    "    print()  # 换行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6ee01ddf-ef64-420c-a8c0-9302b96a81b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "**\n",
      "***\n",
      "****\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "rows = 5\n",
    "for i in range(1, rows + 1):\n",
    "    print(\"*\" * i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dbdfaf00-4028-4881-87b0-62d9faf6693a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    *\n",
      "   ***\n",
      "  *****\n",
      " *******\n",
      "*********\n",
      " *******\n",
      "  *****\n",
      "   ***\n",
      "    *\n"
     ]
    }
   ],
   "source": [
    "rows = 5\n",
    "for i in range(1, rows + 1):\n",
    "    print(\" \" * (rows - i) + \"*\" * (2 * i - 1))\n",
    "for i in range(rows - 1, 0, -1):\n",
    "    print(\" \" * (rows - i) + \"*\" * (2 * i - 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c77cffa0-1777-4201-b531-b7c3be2abbce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1\t\n",
      "1 * 2 = 2\t2 * 2 = 4\t\n",
      "1 * 3 = 3\t2 * 3 = 6\t3 * 3 = 9\t\n",
      "1 * 4 = 4\t2 * 4 = 8\t3 * 4 = 12\t4 * 4 = 16\t\n",
      "1 * 5 = 5\t2 * 5 = 10\t3 * 5 = 15\t4 * 5 = 20\t5 * 5 = 25\t\n",
      "1 * 6 = 6\t2 * 6 = 12\t3 * 6 = 18\t4 * 6 = 24\t5 * 6 = 30\t6 * 6 = 36\t\n",
      "1 * 7 = 7\t2 * 7 = 14\t3 * 7 = 21\t4 * 7 = 28\t5 * 7 = 35\t6 * 7 = 42\t7 * 7 = 49\t\n",
      "1 * 8 = 8\t2 * 8 = 16\t3 * 8 = 24\t4 * 8 = 32\t5 * 8 = 40\t6 * 8 = 48\t7 * 8 = 56\t8 * 8 = 64\t\n",
      "1 * 9 = 9\t2 * 9 = 18\t3 * 9 = 27\t4 * 9 = 36\t5 * 9 = 45\t6 * 9 = 54\t7 * 9 = 63\t8 * 9 = 72\t9 * 9 = 81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j} * {i} = {i * j}\", end=\"\\t\")\n",
    "    print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8e86aec7-9749-41d8-b1d1-3dc14760b286",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 "
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "count = 10\n",
    "while count > 0:\n",
    "    print(a, end=\" \")\n",
    "    a, b = b, a + b\n",
    "    count -= 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f42389f2-3ce5-497a-a5ba-3c22e881ab04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个非负整数： 78\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "78 的阶乘是 11324281178206297831457521158732046228731749579488251990048962825668835325234200766245086213177344000000000000000000\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的数字\n",
    "n = int(input(\"请输入一个非负整数：\"))\n",
    "\n",
    "# 初始化阶乘结果为1\n",
    "factorial = 1\n",
    "\n",
    "# 使用for循环计算阶乘\n",
    "for i in range(1, n + 1):\n",
    "    factorial *= i\n",
    "\n",
    "# 输出结果\n",
    "print(f\"{n} 的阶乘是 {factorial}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b3d19fa9-caff-41fb-8490-340495d2fc49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入底数： 2\n",
      "请输入指数： 255\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 的 255 次幂是 57896044618658097711785492504343953926634992332820282019728792003956564819968\n"
     ]
    }
   ],
   "source": [
    "# 获取用户输入的底数和指数\n",
    "base = int(input(\"请输入底数：\"))\n",
    "exponent = int(input(\"请输入指数：\"))\n",
    "\n",
    "# 初始化结果为1\n",
    "result = 1\n",
    "\n",
    "# 使用for循环计算幂运算\n",
    "for i in range(exponent):\n",
    "    result *= base\n",
    "\n",
    "# 输出结果\n",
    "print(f\"{base} 的 {exponent} 次幂是 {result}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e4a0bac-5f19-4c83-896c-923ba779ab71",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
